package cn.com.qc.leeactivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.qc.R;
import cn.com.qc.adapter.CompanyInfosAdapter;
import cn.com.qc.adapter.JobAdapter;
import cn.com.qc.help.NetUrl;
import cn.com.qc.javabean.CompanyInfos;
import cn.com.qc.javabean.JobInfo;
import cn.com.qc.main.BaseActivity;
import cn.com.qc.main.WebActivity;
import cn.com.qc.url.URLConst;
import cn.com.qc.utils.CustomListView;
import cn.com.qc.utils.Tools;

/**
 * 公司信息
 */

public class CompanyInfosActivity extends BaseActivity {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.company)
    TextView company;
    @BindView(R.id.companypeople)
    TextView companypeople;
    @BindView(R.id.companystyle)
    TextView companystyle;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.location)
    TextView location;
    @BindView(R.id.ctime)
    TextView ctime;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.email)
    TextView email;
    @BindView(R.id.listView)
    CustomListView listView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    private List<JobInfo> jobInfoList;
    private JobAdapter jobAdapter;
    private String id = "";
    private String chttp = "";
    private String cname = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_companyinfos);
        ButterKnife.bind(this);
        Tools.initSystemBarTint(this);
        id = getIntent().getStringExtra("id");
        scrollView.scrollTo(0, 0);
        jobInfoList = new ArrayList<>();
        initData();
        GetJobBycompany_id();
        jobAdapter = new JobAdapter(jobInfoList, this);
        listView.setAdapter(jobAdapter);
    }

    private void initData() {
        OkGo.<String>post(NetUrl.DNS + NetUrl.CompanyDetails)
                .tag(this)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            System.out.println(response.body());
                            JSONObject jsonObject = new JSONObject(response.body());
                            int status = jsonObject.getInt("status");
                            if (status == 0) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                cname = data.optString("cname");
                                chttp = data.optString("chttp");
                                JSONArray profession = data.getJSONArray("profession");
                                String character = "";
                                for(int j = 0;j<profession.length();j++){
                                    JSONObject profession_item = profession.getJSONObject(j);
                                    String name = profession_item.getString("name");
                                    character = character+name+"、";
                                }
                                company.setText(cname.toString().trim());
                                companypeople.setText(data.optString("scale")+"人");
                                companystyle.setText(character);
                                content.setText(data.optString("introduce"));
                                location.setText("公司地址："+data.optString("address"));
                                ctime.setText("公司创建时间："+data.optString("ctime"));
                                phone.setText("联系方式："+data.optString("phone"));
                                email.setText("邮箱地址："+data.optString("email"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    public void GetJobBycompany_id(){
        OkGo.<String>post(NetUrl.DNS + NetUrl.Joblist)
                .tag(this)
                .params("companyId", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            int infoCode = jsonObject.getInt("status");
                            if (infoCode == 0) {
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                                JSONArray jsonArray = jsonObject1.getJSONArray("records");
                                System.out.println("返回的数据是" + jsonArray);
                                if (jsonArray.length() == 0) {

                                } else {
                                    jobInfoList = new ArrayList<>();
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                                        JobInfo jobInfo = new JobInfo();
                                        jobInfo.setLocation(jsonObject2.getString("workPlace"));
                                        jobInfo.setDate(jsonObject2.getString("startTime"));
//                                        String beginDate = jsonObject2.getString("createTime");
//                                        jobInfo.setDate(total_pagesTools.getStrTime(beginDate));
                                        jobInfo.setCompany(jsonObject2.getString("cname"));
                                        jobInfo.setId(jsonObject2.getString("id"));
                                        jobInfo.setJob(jsonObject2.getString("title"));
                                        jobInfo.setPrice(jsonObject2.getInt("minMoney") + "-" + jsonObject2.getInt("maxMoney"));
                                        jobInfoList.add(jobInfo);
                                    }
                                    jobAdapter.addData(jobInfoList);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    public void Toweb(View view){
        Intent intent= new Intent(this, WebActivity.class);
        intent.putExtra("url",chttp);
        intent.putExtra("title",cname);
        startActivity(intent);
    }
    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
